define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'meeting.index/index' + location.search,
                    add_url: 'meeting.index/add',
                    edit_url: 'meeting.index/edit',
                    del_url: 'meeting.index/del',
                    multi_url: 'meeting.index/multi',
                    import_url: 'meeting.index/import',
                    table: 'meeting',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'address', title: __('Address'), operate: 'LIKE'},
                        {field: 'datetime', title: __('Datetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'team_id', title: __('组团'),
                            operate: 'select',
                            formatter: function (value, row, index) {
                                // 直接返回 team_name 值，如果不存在则返回原始值
                                return row.team_name ? row.team_name : (value ? value : '');
                            },
                            searchList: $.getJSON("user/team/selectpage")

                        },

                        {field: 'all', title: __('应到'), operate: false},
                        {field: 'in', title: __('实到'), operate: false},

                        {field: 'weigh', title: __('Weigh'), operate: false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate, buttons: [
                            {
                                name: 'detail',
                                text: __('打卡'),
                                title: __('详情'),
                                classname: 'btn btn-xs btn-info btn-dialog',
                                icon: 'fa fa-list',
                                url: 'meeting.index/detail',
                                visible: function (row) {
                                    return true;
                                }
                            },
                                {
                                    name: 'detail',
                                    text: __('图片'),
                                    classname: 'btn btn-xs btn-info btn-dialog',
                                    url: 'meeting.index/getqrcode',
                                    success: function (data, ret) {
                                       location.reload()
                                    },
                                        visible: function (row) {
                                        return true;
                                    },

                                }
                            ]}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        detail: function () {
            var table = $("#table");
            var id=$('#ids').val();

            var url='meeting/index/setsing?metid='+id;
            // 初始化表格
            table.bootstrapTable({
                url: 'meeting/index/getlist?ids='+id,
                pk: 'id',
                sortName: 'weigh',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'nickname', title: __('用户名')},
                        {field: 'addtime', title: __('打卡时间')},
                        {field: 'is_signt', title: __('状态')},
                        {
                            field: 'buttons',
                            title: __('操作'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [

                                {
                                    name: 'ajax',
                                    text: __('签到'),
                                    title: __('签到详情'),
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    icon: 'fa fa-magic',
                                    url: url,
                                    confirm: '确认签到',
                                    success: function (data, ret) {
                                        location.reload();
                                    },
                                    visible: function (row) {
                                        console.log(row);
                                        if (row.is_sign==0){
                                            return true;
                                        }else {
                                            return false;
                                        }

                                    }

                                }
                            ],
                            formatter: Table.api.formatter.buttons

                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
